package com.atguigu.educms.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.cron.vo.ContentQueryVo;
import com.atguigu.educms.entity.CmsContent;
import com.atguigu.educms.entity.CmsLinks;
import com.atguigu.educms.service.CmsContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章详情表(后台) 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/educms/cms-content")
public class CmsContentController {

    @Autowired
    private CmsContentService contentService;

    @ApiOperation(value = "获取文章列表")
    @PostMapping("/pageContentConditions/{pageNo}/{pageSize}")
    public R pageContentConditions(@PathVariable("pageNo") Integer pageNo,
                                   @PathVariable("pageSize") Integer pageSize,
                                   @RequestBody(required = false) ContentQueryVo contentQueryVo) {
        PageListBean pageListBean = contentService.pageContentConditions(pageNo, pageSize, contentQueryVo);
        return R.ok().data(pageListBean);
    }

    @ApiOperation(value = "获取文章列表---前台显示")
    @GetMapping("/getContentList/{pageNo}/{pageSize}")
    public R getContentList(@PathVariable("pageNo") Integer pageNo,
                            @PathVariable("pageSize") Integer pageSize) {
        FrontPageBean frontPageBean = contentService.getContentList(pageNo,pageSize);
        return R.ok().data(frontPageBean);
    }


    @ApiOperation(value = "获取文章列排行榜--根据评论数---前台显示")
    @GetMapping("/getHotContent")
    public R getHotContent() {
        List<CmsContent> cmsContents = contentService.getHotContent();
        return R.ok().data(cmsContents);
    }

    @ApiOperation(value = "获取文章信息")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Integer id) {
        CmsContent content = contentService.getById(id);
        return R.ok().data("content", content);
    }


    @ApiOperation(value = "修改文章信息")
    @PostMapping("/edit")
    public R edit(@RequestBody CmsContent cmsContent) {
        try {
            contentService.updateById(cmsContent);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "新增文章信息")
    @PostMapping("/add")
    public R add(@RequestBody CmsContent cmsContent) {
        try {
            contentService.save(cmsContent);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "删除文章信息")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        try {
            contentService.removeById(id);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<String> idList) {
        contentService.removeByIds(idList);
        return R.ok();
    }
}
